package xml;

import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class XmlTest {
    @Test
    public void test1() throws ParserConfigurationException, IOException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得Dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即Dom树
        Document parse = db.parse("src/xml/xml/TextHide.xml");
        //得到所有的Brand节点列表信息
        NodeList brandList = parse.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i = 0; i < brandList.getLength(); i++) {
            //获取第i个Brand元素信息
            Node brand = brandList.item(i);
            //获取第i个Brand元素的name属性的值
            Element element = (Element) brand;
            String attribute = element.getAttribute("name");
            NodeList types = element.getChildNodes();
            for (int j = 0; j < types.getLength(); j++) {
                Node item = types.item(j);
                if (item.getNodeType()==Node.ELEMENT_NODE){
                    Element typeElement = (Element) item;
                    String type = typeElement.getAttribute("name");
                    System.out.println("手机：" + attribute + type);

                }

            }
        }


    }
    @Test
    public void test2() throws ParserConfigurationException, IOException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得Dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即Dom树
        Document parse = db.parse("src/xml/xml/Favorites.xml");
        //得到所有的Brand节点列表信息
        NodeList list = parse.getElementsByTagName("pubDate");
        //pubDate元素节点
        Element item = (Element) list.item(0);
        //读取文本节点
        String pubDate = item.getFirstChild().getNodeValue();
        System.out.println(pubDate);

    }
    @Test
    public void test3() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得Dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即Dom树
        Document parse = db.parse("src/xml/xml/TextHide.xml");
        //创建brand节点
        Element brand = parse.createElement("Brand");
        brand.setAttribute("name","三星");
        //创建type节点
        Element type = parse.createElement("Type");
        type.setAttribute("name","NoteX");
        //添加父子关系
        brand.appendChild(type);
        Element phoneElement = (Element) parse.getElementsByTagName("PhoneInfo").item(0);
        phoneElement.appendChild(brand);
        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(parse);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result = new StreamResult(new FileOutputStream("src/xml/xml/TextHide.xml"));
        //把DOM树转换XML文件
        transformer.transform(domSource,result);

    }
    @Test
    public void test4() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得Dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即Dom树
        Document parse = db.parse("src/xml/xml/TextHide.xml");
        //找到修改的节点
        NodeList brand = parse.getElementsByTagName("Brand");
        for (int i = 0; i < brand.getLength(); i++) {
            Element brandElement = (Element) brand.item(i);
            String attribute = brandElement.getAttribute("name");
            if (brandElement.equals("三星")){
                brandElement.setAttribute("name","SAMSUNG");
            }

        }
        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(parse);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result = new StreamResult(new FileOutputStream("src/xml/xml/TextHide.xml"));
        //把DOM树转换XML文件
        transformer.transform(domSource,result);
    }
    @Test
    public void test5() throws ParserConfigurationException, TransformerException, IOException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得Dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即Dom树
        Document parse = db.parse("src/xml/xml/TextHide.xml");
        //找到删除的节点
        NodeList brand = parse.getElementsByTagName("Brand");
        for (int i = 0; i < brand.getLength(); i++) {
            Element brandElement = (Element) brand.item(i);
            String attribute = brandElement.getAttribute("name");
            if (brandElement.equals("三星")){
                brandElement.getParentNode().removeChild(brandElement);
            }

        }
        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(parse);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");
        StreamResult result = new StreamResult(new FileOutputStream("src/xml/xml/TextHide.xml"));
        //把DOM树转换XML文件
        transformer.transform(domSource,result);
    }




}
